__author__ = "Ypf"
_date_ = "2019/8/29-16:43"



from common.logger_helper import g_wlogger
from common.pandas_helper import PandasHelper
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.config import bussiness_cal_fields
from k3_data_report.finance.cpbfy_db import CpbFy
from k3_data_report.finance.xsbfy_db import XsbFy



def cpbfy(time1):
    yea=time1.split('-')[0]
    print (yea)
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3skjllr', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic) == 0:
        cpb_sql=db_sql["cpbfyhs_jd_sql"]
        cpb_che_sql = db_sql["cpbch_jd_sql"]
        insert_sql = db_sql["cpbfyhs_jd_isnert"]
        cp_name=["王祝良","谢金堂","熊国明","徐少平","张文东"]
        dic_xsch = {}
        dic_fybx = {}
        for name in cp_name:
            jd_dic = CpbFy.get_jd_dic(cpb_sql, name)
            dic_fybx.update(jd_dic)
            jd_dic = CpbFy.get_jdch_dic(cpb_che_sql, name,yea)
            dic_xsch.update(jd_dic)
        for key2 in dic_xsch:
            dic_xsch[key2] = dic_xsch[key2] + dic_fybx[key2]
        XsbFy.insert_xsch(insert_sql, dic_xsch, time1)
    else:
        g_wlogger.winfo(f"cpbfy-{time1}:target_full_df, all data is empty.")
def create_df_cpbfy(time1=None):
    yea=time1.split('-')[0]
    print (yea)
    db_sql = bussiness_cal_fields['k3_caozuo']
    cpb_sql=db_sql["cpbfyhs_jd_sql"]
    cpb_che_sql = db_sql["cpbch_jd_sql"]
    cp_name=["王祝良","谢金堂","熊国明","徐少平","张文东"]
    cpbfy_dic_xsch = {}
    dic_fybx = {}
    for name in cp_name:
        jd_dic = CpbFy.get_jd_dic(cpb_sql, name)
        dic_fybx.update(jd_dic)
        jd_dic = CpbFy.get_jdch_dic(cpb_che_sql, name,yea)
        cpbfy_dic_xsch.update(jd_dic)
    for key2 in cpbfy_dic_xsch:
        cpbfy_dic_xsch[key2] = cpbfy_dic_xsch[key2] + dic_fybx[key2]
    list_out = []
    for key in cpbfy_dic_xsch:
        dic_xsch = cpbfy_dic_xsch.get(key, None)
        if dic_xsch == None:
            continue
        print(dic_xsch)
        list_ap=[key,"产品经理","","","","",dic_xsch[0],"","",
               dic_xsch[4],"",dic_xsch[1],"","",
               dic_xsch[5],"",dic_xsch[2],"","",
               dic_xsch[6],"",dic_xsch[3],"","",
               dic_xsch[7],float(dic_xsch[4])+float(dic_xsch[5])+float(dic_xsch[6])+float(dic_xsch[7]),"",""]
        list_out.append(list_ap)
    #     q1sjch,q2sjch,q3sjch,q4sjch,q1sjfy,q2sjfy,q3sjfy,q4sjfy
    columns = ["人员","职位",f"{yea}年业绩指标",f"{yea}年总计划费用","预计有效毛利率","Q1计划出货","Q1实际出货","任务完成率","Q1预算费用",
               "Q1实际费用","Q2计划出货","Q2实际出货","任务完成率","Q2预算费用",
               "Q2实际费用","Q3计划出货","Q3实际出货","任务完成率","Q3预算费用",
               "Q3实际费用","Q4计划出货","Q4实际出货","任务完成率","Q4预算费用",
               "Q4实际费用","年度实际费用","与预算费用差额","年度任务完成率"]
    k3cpbfy_df = PandasHelper.create_dataframe(data=list_out, columns=columns)
    return k3cpbfy_df
def cpbfy_main(month):
    try:
        cpbfy(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"cpbfy_main has error:{e} with:{month}")
        print(f"cpbfy_main has error:{e} with:{month}")
        return False



